package com.googlecode.gwtbb.client.only.widget.basic;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.googlecode.gwtbb.client.only.GwtbbCmess;
import com.googlecode.gwtbb.client.shared.entity.picture.Picture;
import com.googlecode.gwtbb.client.shared.entity.picture.Thumb;
import com.googlecode.gwtbb.client.shared.util.StringUtils;
import com.googlecode.gwtbb.client.shared.util.PictureUtils;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Dec 29, 2008
 */
public class PictureFileUpload extends AsyncFileUploadBase {
	private static final IconBundle icons = GwtbbBundle.icons();
	public PushButton deleteButton;
	private Widget statusEmptyWidget;
	private String addPictureText;
	private Button addPictureButton;
	private CaptionFetcher captionFetcher;
	private String caption;
	private Image pictureThumb;
	private TooltipMouseListener thumbTooltip;
	private VerticalPanel buttonPanel;

	private AsyncCallback<String> fetchCaptionCallback = new AsyncCallback<String>() {
		public void onFailure(Throwable caught) {
			setCaption(null);
			if (thumbTooltip != null)
				pictureThumb.removeMouseListener(thumbTooltip);
		}

		public void onSuccess(String caption) {
			addCaptionTooltip(caption);
		}
	};

	public PictureFileUpload(String formAction, CaptionFetcher captionFetcher, String addPictureText) {
		super(formAction);
		this.captionFetcher = captionFetcher;
		setAddPictureText(addPictureText);

		// for use in uploading and uploaded widgets
		buttonPanel = new VerticalPanel();

		deleteButton = new PushButton(icons.close().createImage());
		deleteButton.setEnabled(false);
		buttonPanel.add(deleteButton);
		deleteButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				deleteUpload();
			}
		});
		PushButton editButton = new PushButton(icons.edit().createImage());
		buttonPanel.add(editButton);
		editButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				activateFetchCaption();
			}
		});
	}

	public String getAddPictureText() {
		return addPictureText;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	public Widget getStatusDeletedWidget() {
		VerticalPanel statusDeletedWidget = new VerticalPanel();
		statusDeletedWidget.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		statusDeletedWidget.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		Button addAnotherPicture = new Button(GwtbbCmess.instance().addAnotherPicture());
		statusDeletedWidget.add(addAnotherPicture);
		addAnotherPicture.addClickListener(promptFileUploadClickListener);
		Button reAddPicture = new Button(GwtbbCmess.instance().reAddPicture(getShortClientFileName()));
		statusDeletedWidget.add(reAddPicture);
		reAddPicture.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				fakeUpload(getServerFileName(), getAdditionalInfo());
			}
		});
		return statusDeletedWidget;
	}

	public Widget getStatusEmptyWidget() {
		if (statusEmptyWidget == null) {
			addPictureButton = new Button(addPictureText);
			addPictureButton.addClickListener(promptFileUploadClickListener);
			statusEmptyWidget = addPictureButton;
		}
		return statusEmptyWidget;
	}

	public void activateFetchCaption() {
		captionFetcher.fetchCaption(caption, fetchCaptionCallback);
	}

	public void addSideWidget(Widget widget) {
		buttonPanel.add(widget);
	}

	public Widget getSideWidget(int index) {
		// 2 is how many buttons appear by default at the side... delete and edit
		return buttonPanel.getWidget(index + 2);
	}

	public Widget getStatusUploadedWidget() {
		DockPanel dockPanel = new DockPanel();
		pictureThumb = new Image(getThumbUrl());
		dockPanel.add(pictureThumb, DockPanel.CENTER);
		if (thumbTooltip != null)
			pictureThumb.addMouseListener(thumbTooltip);
		dockPanel.add(buttonPanel, DockPanel.EAST);
		return dockPanel;
	}

	public Widget getStatusUploadingWidget() {
		DockPanel dockPanel = new DockPanel();
		VerticalPanel loadingPic = new VerticalPanel();
		dockPanel.add(loadingPic, DockPanel.CENTER);
		loadingPic.setStylePrimaryName("gwtbb-PictureFileUpload-loadingPic");
		loadingPic.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		loadingPic.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		loadingPic.add(new LoadingImage());
		loadingPic.add(new Label(GwtbbCmess.instance().loadingTarget(getShortClientFileName())));
		dockPanel.add(buttonPanel, DockPanel.EAST);
		return dockPanel;
	}

	public String getThumbUrl() {
		return getAdditionalInfo();
	}

	public void onDeleted() {
		deleteButton.setEnabled(false);
	}

	public void onUploaded() {
		deleteButton.setEnabled(true);
	}

	public void onUploading(String fileName) {
	}

	public void setAddPictureText(String addPictureText) {
		this.addPictureText = addPictureText;
		if (addPictureButton != null)
			addPictureButton.setText(addPictureText);
	}

	public void setPicture(Picture picture) {
		String serverFileName = PictureUtils.getServerFileName(picture);
		Thumb primaryThumb = picture.getPrimaryThumb();
		String additionalInfo = primaryThumb != null ? primaryThumb.getUrl() : null;
		fakeUpload(serverFileName, additionalInfo);
		addCaptionTooltip(picture.getCaption());
	}

	private void addCaptionTooltip(String caption) {
		setCaption(caption);
		if (thumbTooltip != null)
			pictureThumb.removeMouseListener(thumbTooltip);
		if (StringUtils.isNotEmpty(caption)) {
			thumbTooltip = new TooltipMouseListener(caption);
			if (pictureThumb != null)
				pictureThumb.addMouseListener(thumbTooltip);
		}
	}
}
